package com.woniuxy.tfss.web.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.tfss.entity.Perms;
import com.woniuxy.tfss.entity.ResponseEntity;
import com.woniuxy.tfss.entity.Role;
import com.woniuxy.tfss.entity.RolePerm;
import com.woniuxy.tfss.service.PermsService;
import com.woniuxy.tfss.service.RolePermService;
import com.woniuxy.tfss.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.Get;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;
import java.util.List;

@Controller("adminRoleController")
@RequestMapping("admin/role")
@CrossOrigin
@Slf4j
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private RolePermService rolePermService;
    @Autowired
    private PermsService permsService;

    /**
     * 查询所有的角色
     * @return
     */
    @GetMapping("list")
    @ResponseBody
    public ResponseEntity<List<Role>> getRole(){
        List<Role> roles=roleService.list();
        return new ResponseEntity<>(roles);
    }

    /**
     * 角色多条件分页查询
     * @param name
     * @param permsId
     * @param status
     * @param page
     * @return
     */
    @GetMapping("listByPage")
    @ResponseBody
    public ResponseEntity<Page<Role>> listByPage(String name, Integer[] permsId,String status,Integer page){
        QueryWrapper<Role> wrapper=new QueryWrapper<>();
        QueryWrapper<RolePerm> wrapper1=new QueryWrapper<>();
        if(!StringUtils.isEmpty((name))){
            wrapper.like("name",name);
        }else if(!StringUtils.isEmpty((status))&& !"0".equals(status)){
            wrapper.eq("status",status);
        }else if(permsId!=null && permsId.length!=0){
            for(Integer permId : permsId){
                wrapper1.or().eq("permId",permId);
            }
            List<RolePerm> rolePerms =rolePermService.list(wrapper1);
            for(RolePerm rolePerm :rolePerms){
                wrapper.or().eq("id",rolePerm.getRoleId());
            }
        }
        Page rolePage=new Page(page==null?1:page,3);
        return new ResponseEntity<>(roleService.page(rolePage,wrapper));
    }

    /**
     * 增加角色
     * @param name
     * @param description
     * @param permsId
     * @return
     */
    @PostMapping("add")
    @ResponseBody
    public ResponseEntity<Void> add(String name,String description,Integer[] permsId){
        Role role=new Role();
        role.setName(name);
        role.setDescription(description);
        role.setStatus(Role.NORMAL);
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("name",role.getName());
        if(roleService.getOne(wrapper)==null){//角色名字不能重复
            roleService.add(role);
            RolePerm rolePerm=new RolePerm();
            rolePerm.setRoleId(role.getId());
            for(Integer permId :permsId){
                rolePerm.setPermId(permId);
                rolePermService.save(rolePerm);
            }
            return ResponseEntity.SUCCESS;
        }else{
            return ResponseEntity.EXISTROLE;
        }
    }

    /**
     * 查看角色详情（权限）
     * @param id
     * @return
     */
    @GetMapping("view")
    @ResponseBody
    public ResponseEntity view(Integer id){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("roleId",id);
        List<RolePerm> rolePerms=rolePermService.list(wrapper);
        if(rolePerms==null || rolePerms.size()==0){
           return ResponseEntity.SUCCESS;
        }
        QueryWrapper<Perms> wrapper1=new QueryWrapper<>();
        for(RolePerm rolePerm : rolePerms){
            wrapper1.or().eq("id",rolePerm.getPermId());
        }
        List<Perms> perms=permsService.list(wrapper1);
        return new ResponseEntity<>(perms);
    }

    /**
     * 修改角色信息及权限
     * @param id
     * @param name
     * @param status
     * @param description
     * @param permsId
     * @return
     */
    @PostMapping("update")
    @ResponseBody
    public ResponseEntity<Void> update(Integer id,String name,String status,String description,Integer[] permsId){
        Role role=new Role();
        role.setId(id);
        role.setName(name);
        role.setDescription(description);
        role.setStatus(status);
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("name",role.getName());
        QueryWrapper wrapper1=new QueryWrapper();
        wrapper1.eq("id",role.getId());
        QueryWrapper wrapper2=new QueryWrapper();
        wrapper2.eq("roleId",role.getId());
        Role role1=roleService.getOne(wrapper);
        if(role1!=null){
            if(role1.getId()!=role.getId()){
                return ResponseEntity.EXISTROLE;
            }
        }
            roleService.update(role,wrapper1);
            RolePerm rolePerm=new RolePerm();
            rolePerm.setRoleId(role.getId());
            rolePermService.remove(wrapper2);
            if(permsId!=null&&permsId.length!=0) {
                for (Integer permId : permsId) {
                    rolePerm.setPermId(permId);
                    rolePermService.save(rolePerm);
                }
            }
            return ResponseEntity.SUCCESS;
    }
}
